package cn.org.timespace.ons.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ExcelFileFilter {

	private final static Log log = LogFactory.getLog(ExcelFileFilter.class);
	
	private Workbook workbook = null;
	
	private Sheet sheet = null;
	
	public Sheet getSheet(InputStream in) {
		try
		{
			workbook = Workbook.getWorkbook(in);
			sheet = workbook.getSheet(0);
		}
		catch (Exception e)
		{
			log.error("解析上传文件出错", e);
		}
		
		return sheet;
	}
	
	public int getRows() {
		return sheet.getRows();
	}
	
	public int getCols() {
		return sheet.getColumns();
	}
	
	public List<String[]> filter() {
		List<String[]> dataOfFile = new ArrayList<String[]>();
		try
		{
			int rows = sheet.getRows();
			int cols = sheet.getColumns();
			for (int i = 1; i < rows; i++)
			{
				String[] rowData = new String[cols];
				dataOfFile.add(rowData);
				for (int j = 0; j < cols; j++)
				{
					Cell cell = sheet.getCell(j, i);
					rowData[j] = cell.getContents().trim();
				}
			}
		}
		catch (Exception e)
		{
			log.error("解析上传文件出错", e);
		}
		
		return dataOfFile;
	}
	
	public void closeWorkbook() {
		if(workbook != null)
			workbook.close();
	}
}
